#include "exporter.h"

Exporter::Exporter(QObject *parent) :
    QObject(parent)
{
    mixchan = BASS_Mixer_StreamCreate(44100, 2, BASS_STREAM_DECODE);
    msg = new QMessageBox();
    msg->setText("Сохранено!");
}

void Exporter::write(QString fileName)
{
    if (fileName == "") return;
    this->fileName = fileName;
    emit channels();
}

void Exporter::write()
{
    for (int i = 0; i < chans.size(); i++) {
        BASS_Mixer_StreamAddChannel(mixchan, chans[i], BASS_STREAM_DECODE);
    }

    BASS_Encode_Start(mixchan, fileName.toLocal8Bit().data(), BASS_ENCODE_PCM, NULL, 0);
    QWORD pos = 0;
    QWORD tempPos = 1;
    while (tempPos != pos) {
            char temp[20000];
            BASS_ChannelGetData(mixchan,temp,20000);
            tempPos = pos;
            pos = BASS_ChannelGetPosition(mixchan, BASS_POS_BYTE);
        }
    BASS_Encode_Stop(mixchan);
    for (int i = 0; i < chans.size(); i++) {
        BASS_Mixer_ChannelRemove(chans[i]);
    }
    for (int i = 0; i < chans.size(); i++) {
        BASS_ChannelSetPosition(chans[i], 0, BASS_POS_BYTE);
    }
    BASS_ChannelSetPosition(mixchan, 0, BASS_POS_BYTE);
    msg->exec();
}

void Exporter::getChannels(QVector<HSTREAM> chans)
{
    if (chans.size() == 0) return;
    this->chans = chans;
    write();
}
